package com.example.pc.mytestapplication.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.widget.AdapterView;

import com.example.pc.mytestapplication.R;
import com.example.pc.mytestapplication.adapter.AdapterMain;
import com.example.pc.mytestapplication.data.MainData;
import com.example.pc.mytestapplication.databinding.ActivityGroupBinding;
import com.example.pc.mytestapplication.test.TestGroupActivity;
import com.example.pc.mytestapplication.util.TitleBarUtils;
import com.example.pc.mytestapplication.util.YZLog;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import io.reactivex.Maybe;
import io.reactivex.MaybeSource;
import io.reactivex.MaybeTransformer;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiConsumer;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class GroupActivity extends BaseActivity {

    private ActivityGroupBinding binding;
    private List<MainData> mainData;
    private AdapterMain adapterMain;

    @Override
    protected void loadView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_group);
        TitleBarUtils.setTitle(GroupActivity.this, "组合操作符", true);
    }

    @Override
    protected void setinitView() {
        binding.btGroup.setOnClickListener(this);
        binding.btTestGroup.setOnClickListener(this);
        binding.lvConverion.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                switch (i) {
                    case 0:
                        Observable.concat(Observable.just(0L, 1L, 2L, 3L, 4L)
                                , Observable.intervalRange(0, 6, 1, 2, TimeUnit.SECONDS)
                                , Observable.just(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L)
                        )
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new io.reactivex.Observer<Long>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(Long s) {
                                        if (mainData.get(i).getTextType().startsWith("未处理")) {
                                            mainData.get(i).setTextType("");
                                        }
                                        mainData.get(i).setTextType(mainData.get(i).getTextType() + "  |  " + s);
                                        adapterMain.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onComplete() {
                                        adapterMain.notifyDataSetChanged();
                                    }
                                });

                        break;
                    case 1:
                        Observable.merge(Observable.just(0L, 1L, 2L, 3L, 4L)
                                , Observable.intervalRange(0, 6, 1, 2, TimeUnit.SECONDS)
                                , Observable.just(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L)
                        )
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new io.reactivex.Observer<Long>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(Long aLong) {
                                        if (mainData.get(i).getTextType().startsWith("未处理")) {
                                            mainData.get(i).setTextType("");
                                        }
                                        mainData.get(i).setTextType(mainData.get(i).getTextType() + "  |  " + aLong + "\n");
                                        adapterMain.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });
                        break;
                    case 2:
                        Observable.concatArrayDelayError(Observable.create(new ObservableOnSubscribe<Integer>() {
                                    @Override
                                    public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                                        emitter.onNext(1);
                                        emitter.onNext(2);
                                        emitter.onError(new RuntimeException());
                                        emitter.onNext(3);
                                        emitter.onComplete();
                                    }
                                })
                                , Observable.just(4, 5, 6)
                                        .subscribeOn(Schedulers.io())
                                        .observeOn(AndroidSchedulers.mainThread()))
                                .subscribe(new Observer<Integer>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(Integer integer) {
                                        if (mainData.get(i).getTextType().startsWith("未处理")) {
                                            mainData.get(i).setTextType("");
                                        }
                                        mainData.get(i).setTextType(mainData.get(i).getTextType() + "  |  " + integer + "\n");
                                        adapterMain.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        if (mainData.get(i).getTextType().startsWith("未处理")) {
                                            mainData.get(i).setTextType("");
                                        }
                                        mainData.get(i).setTextType(mainData.get(i).getTextType() + e.getClass().getSimpleName());
                                        adapterMain.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onComplete() {
                                        if (mainData.get(i).getTextType().startsWith("未处理")) {
                                            mainData.get(i).setTextType("");
                                        }
                                        mainData.get(i).setTextType("事件接收完成" + mainData.get(i).getTextType());
                                        adapterMain.notifyDataSetChanged();
                                    }
                                });
                        break;
                    case 3:
                        Observable<String> stringObservable = Observable.create(new ObservableOnSubscribe<String>() {
                            @Override
                            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                                emitter.onNext("A");
                                emitter.onNext("B");
                                emitter.onNext("C");
                            }
                        }).subscribeOn(Schedulers.io());
                        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
                            @Override
                            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                                emitter.onNext("a");
                                emitter.onNext("b");
                                emitter.onNext("c");
                                emitter.onNext("d");
                            }
                        }).subscribeOn(Schedulers.newThread());
                        Observable.zip(stringObservable, observable, new BiFunction<String, String, String>() {
                            @Override
                            public String apply(String s, String s2) throws Exception {
                                YZLog.i("事件1:" + s + "事件2" + s2);
                                return s + s2;
                            }
                        }).observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<String>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(String s) {
                                        if (mainData.get(i).getTextType().startsWith("未处理")) {
                                            mainData.get(i).setTextType("");
                                        }
                                        mainData.get(i).setTextType(mainData.get(i).getTextType() + "  |  " + s + "\n");
                                        adapterMain.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });
                        break;
                    case 4:
                        Observable.combineLatest(Observable.just(1L, 2L, 3L, 4L).subscribeOn(Schedulers.io()), Observable.intervalRange(0, 6, 1, 1, TimeUnit.SECONDS).subscribeOn(Schedulers.io()), new BiFunction<Long, Long, Long>() {
                            @Override
                            public Long apply(Long aLong, Long aLong2) throws Exception {
                                YZLog.i("拿到的事件结果为" + aLong + "    " + aLong2);
                                if (mainData.get(i).getTextType().startsWith("未处理")) {
                                    mainData.get(i).setTextType("");
                                }
                                /*mainData.get(i).setTextType(mainData.get(i).getTextType() + "  接收数据  " + aLong + "    " + aLong2 + "\n");
                                adapterMain.notifyDataSetChanged();*/
                                return aLong + aLong2;
                            }
                        })
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Observer<Long>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {
                                        YZLog.i("combinelatest__onSubscribe");
                                    }

                                    @Override
                                    public void onNext(Long aLong) {
                                        YZLog.i("combinelatest__onNext");
                                        mainData.get(i).setTextType(mainData.get(i).getTextType() + "  合并结果  " + aLong + "\n");
                                        adapterMain.notifyDataSetChanged();
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        YZLog.i("combinelatest__onError" + e.getClass().getSimpleName());
                                        e.printStackTrace();
                                    }

                                    @Override
                                    public void onComplete() {
                                        YZLog.i("combinelatest__onComplete");
                                    }
                                });
                        break;
                    case 5:
                        Observable
                                .just(1, 2, 3, 4)
                                .reduce(new BiFunction<Integer, Integer, Integer>() {
                                    @Override
                                    public Integer apply(Integer integer, Integer integer2) throws Exception {
                                        return integer * integer2;
                                    }
                                })
                                .compose(GroupActivity.this.<Integer>bindUntilEvent(ActivityEvent.DESTROY))
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<Integer>() {
                                    @Override
                                    public void accept(Integer o) throws Exception {
                                        mainData.get(i).setTextType("执行结果为:" + o.toString());
                                        adapterMain.notifyDataSetChanged();
                                    }
                                });
                        break;
                    case 6:
                        /*Observable
                                .just(1, 2, 3, 4)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .compose(GroupActivity.this.<Integer>bindUntilEvent(ActivityEvent.DESTROY))
                                .collect(new Callable<ArrayList<Integer>>() {
                                    @Override
                                    public ArrayList<Integer> call() throws Exception {
                                        return new ArrayList<>();
                                    }
                                }, new BiConsumer<ArrayList<Integer>, Integer>() {
                                    @Override
                                    public void accept(ArrayList<Integer> list, Integer integer) throws Exception {
                                        list.add(integer);
                                    }
                                }).flatMap(new Function<ArrayList<Integer>, SingleSource<Integer>>() {
                            @Override
                            public SingleSource<Integer> apply(ArrayList<Integer> list) throws Exception {
                                int num = 0;
                                for (int i = 0;i<list.size();i++){
                                    num+=list.get(i);

                                }
                                return ;
                            }
                        })*/
                        break;
                }
            }
        });
    }

    @Override
    protected void loadData() {
        mainData = new ArrayList<>();
        mainData.add(new MainData("这是一个使用concat\\concatArray方法将多个被观察者组合成一个观察者并按Observable顺序执行的事件", "未处理"));
        mainData.add(new MainData("这是一个使用marge\\margeArray方法将多个被观察者组合成一个观察者按并按照onNext执行顺序执行", "未处理"));
        mainData.add(new MainData("这是一个使用concatDelayError\\margeDelayError方法的实例,该方法用于延迟多个Observable处理中发生的onError事件到最后执行", "未处理"));
        mainData.add(new MainData("这是一个使用zip方法的实例,该方法用于将两个不同的观察者进行一对一配对组合后统一交给Observer进行处理", "未处理"));
        mainData.add(new MainData("这是一个使用conbinelatest方法,将第一个Obserable的最后发出的事件,与第二个Observable依次组合处理,如果在某一时间点下,第二个Observable无事件,则单独发出第一个Observable的事件", "未处理"));
        mainData.add(new MainData("这是一个使用reduce方法,将Observable发出的所有OnNext事件进行整合后统一发布的实例", "未处理"));
        mainData.add(new MainData("", "未处理"));
        adapterMain = new AdapterMain(this, mainData);
        binding.lvConverion.setAdapter(adapterMain);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_test_group:
                startActivity(new Intent(GroupActivity.this, TestGroupActivity.class));
                break;
            case R.id.bt_group:
                break;
        }
    }
}